<?php

namespace App\Admin\Metrics\Posts;

use App\Models\Posts\WxPost;
use Dcat\Admin\Widgets\Metrics\RadialBar;
use Illuminate\Http\Request;

class PostsTickets extends RadialBar
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $this->title('笔记');
        $this->height(400);
        $this->chartHeight(300);
        $this->chartLabels('同比增长');
        $this->dropdown([
            '7' => '全部',
//            '30' => '最近30天',
//            '150' => '最近半年',
//            '365' => '最近一年',
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        switch ($request->get('option')) {
            case '365':
            case '30':
            case '28':
            case '7':
            default:
                global $__MINI_GLOBAL_TENANT_ID__;
                // 数量
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    $total = WxPost::where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->count();
                    // 推送中
                    $pushCount = WxPost::where('is_examine', 1)->where('posts_state', 0)->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->count();
                    // 审核中
                    $hiddenCount = WxPost::where('is_examine', 0)->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->count();
                    // 驳回
                    $boCount = WxPost::where('is_examine', 2)->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->count();
                    // 卡片内容
                    $this->withContent($total);
                    // 卡片底部
                    $this->withFooter($pushCount, $hiddenCount, $boCount);
                    // 昨天帖数
                    $yCount = WxPost::whereDate('created_at', date("Y-m-d",strtotime("-1 day")))->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->count();
                    $tCount = WxPost::where('created_at', date('Y-m-d'))->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->count();
                    if($tCount == 0){
                        $rise = 0;
                    }else{
                        $rise = (1 - $yCount / $tCount) * 100;
                    }

                    // 图表数据
                    $this->withChart($rise);
                }else{
                    $total = WxPost::count();
                    // 推送中
                    $pushCount = WxPost::where('is_examine', 1)->where('posts_state', 0)->count();
                    // 审核中
                    $hiddenCount = WxPost::where('is_examine', 0)->count();
                    // 驳回
                    $boCount = WxPost::where('is_examine', 2)->count();
                    // 卡片内容
                    $this->withContent($total);
                    // 卡片底部
                    $this->withFooter($pushCount, $hiddenCount, $boCount);
                    // 昨天帖数
                    $yCount = WxPost::whereDate('created_at', date("Y-m-d",strtotime("-1 day")))->count();
                    $tCount = WxPost::where('created_at', date('Y-m-d'))->count();
                    if($tCount == 0){
                        $rise = 0;
                    }else{
                        $rise = (1 - $yCount / $tCount) * 100;
                    }

                    // 图表数据
                    $this->withChart($rise);
                }

        }
    }

    /**
     * 设置图表数据.
     *
     * @param int $data
     *
     * @return $this
     */
    public function withChart(int $data)
    {
        return $this->chart([
            'series' => [$data],
        ]);
    }

    /**
     * 卡片内容
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex flex-column flex-wrap text-center">
    <h1 class="font-lg-2 mt-2 mb-0 p-1">{$content}</h1>
    <small>篇内容</small>
</div>
HTML);
    }

    /**
     * 卡片底部内容.
     *
     * @param string $new
     * @param string $open
     * @param string $response
     *
     * @return $this
     */
    public function withFooter($new, $open, $response)
    {
        return $this->footer(
            <<<HTML
<div class="d-flex justify-content-between p-1" style="padding-top: 0!important;">
    <div class="text-center">
        <p>推送中</p>
        <span class="font-lg-1">{$new}</span>
    </div>
    <div class="text-center">
        <p>待审核</p>
        <span class="font-lg-1">{$open}</span>
    </div>
    <div class="text-center">
        <p>驳回</p>
        <span class="font-lg-1">{$response}</span>
    </div>
</div>
HTML
        );
    }
}
